// pages/goods/goodsList/goodsList.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parentClassId: null,
        classId: null,
        classInfo: {},
        classImg: null,
        classList: [],
        nowIndex: -1,
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        imgHeight: 0,
        navFixed: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            parentClassId: options.classId,
            classId: options.classId
        })
        this.getClassList()
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            parentClassId: null,
            classId: null,
            classInfo: {},
            classImg: null,
            classList: [],
            nowIndex: -1,
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false,
            imgHeight: 0,
            navFixed: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    onPageScroll: function(e) {
        let query = wx.createSelectorQuery()
        query.select('#fixImg').boundingClientRect((rect) => {
            if (e.scrollTop > rect.height) {
                this.setData({
                    navFixed: true
                })
            } else {
                this.setData({
                    navFixed: false
                })
            }
        }).exec()
    },

    getClassList: function() {
        const that = this
        let data = {
            activity: 785,
            class_id: this.data.parentClassId
        }
        common.post('Goodsclass/get_class', data).then(function(res) {
            if (res.list) {
                that.setData({
                    classInfo: res,
                    classList: res.list,
                    classImg: res.class_img
                })
                if (that.data.classImg) {
                    that.calHeight()
                } else {
                    that.setData({
                        navFixed: true
                    })
                }
            }
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let goodsList = this.data.goodsList
        let data = {
            activity: 785,
            is_goods: "0",
            client: 1,
            class_id: this.data.classId,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.nowIndex == e.currentTarget.dataset.index) {
            return
        }
        this.setData({
            nowIndex: e.currentTarget.dataset.index,
            classId: e.currentTarget.dataset.id,
            classImg: e.currentTarget.dataset.index == -1 ? this.data.classInfo.class_img : this.data.classList[e.currentTarget.dataset.index].class_img,
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false,
            imgHeight: 0,
            navFixed: false
        })
        this.getGoodsList()
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id,
        })
    }
})